How to make beautiful animated plots using gganimate and tweenr

This code is for creating plots that build up over time (i.e., the x-axis)

library(ggplot2)
library(gganimate)
library(animation)
library(tweenr)

First, load your data.

You data should include at least 4 coloumns:

  1. A variable of interest (e.g., accuracy scores), which will be the y-axis

  2. A variable representing time (e.g., trial number), which will be the x-axis

  3. A varaible representing individual data points (e.g., participant number)

  4. A variable representing condition to which individuals are assigned (e.g., male vs. female)

data <- read.csv("animated_plot_data.csv") 

head(data,30) # show the first 30 rows of the data frame
##    Trial.Number Participant Condition Accuracy
## 1             1           7    Female       11
## 2             2           7    Female       46
## 3             3           7    Female       59
## 4             4           7    Female       57
## 5             5           7    Female       64
## 6             6           7    Female       59
## 7             7           7    Female       65
## 8             8           7    Female       67
## 9             9           7    Female       73
## 10           10           7    Female       69
## 11           11           7    Female       68
## 12           12           7    Female       76
## 13           13           7    Female       82
## 14           14           7    Female       75
## 15           15           7    Female       71
## 16           16           7    Female       73
## 17           17           7    Female       73
## 18            1           8    Female       13
## 19            2           8    Female       16
## 20            3           8    Female       32
## 21            4           8    Female       39
## 22            5           8    Female       40
## 23            6           8    Female       46
## 24            7           8    Female       52
## 25            8           8    Female       47
## 26            9           8    Female       60
## 27           10           8    Female       49
## 28           11           8    Female       64
## 29           12           8    Female       58
## 30           13           8    Female       73

Then, use the tweenr package to make a smooth transition between each time point.

The “tween_elements functions will create two additional coloums to your data frame:”.frame" (corresponding to the origina trial number) and “.group” (corresponding to the original participant number).

data$ease <- "linear" #add a coloumn to the data frame

data2<- tween_elements(data, 'Trial.Number', 'Participant', 'ease', nframes = 100) # create 100 frames between each trial number

head(data2,30) # show the first 30 rows of the data frame
##      Trial.Number Condition  Accuracy .frame .group
## 304      1.000000    Female 17.000000      0     10
## 405      1.000000    Female  2.000000      0     11
## 506      1.000000    Female  7.000000      0     12
## 607      1.000000      Male 11.000000      0     13
## 708      1.000000      Male 34.000000      0     14
## 809      1.000000      Male 11.000000      0     15
## 910      1.000000      Male 27.000000      0     16
## 1011     1.000000      Male  6.000000      0     17
## 1112     1.000000      Male 14.000000      0     18
## 1        1.000000    Female 11.000000      0      7
## 102      1.000000    Female 13.000000      0      8
## 203      1.000000    Female  6.000000      0      9
## 305      1.166667    Female 18.333333      1     10
## 406      1.166667    Female  3.000000      1     11
## 507      1.166667    Female 10.166667      1     12
## 608      1.166667      Male 13.500000      1     13
## 709      1.166667      Male 29.000000      1     14
## 810      1.166667      Male 11.333333      1     15
## 911      1.166667      Male 27.833333      1     16
## 1012     1.166667      Male 10.500000      1     17
## 1113     1.166667      Male 14.000000      1     18
## 2        1.166667    Female 16.833334      1      7
## 103      1.166667    Female 13.500000      1      8
## 204      1.166667    Female  7.166667      1      9
## 306      1.333333    Female 19.666667      2     10
## 407      1.333333    Female  4.000000      2     11
## 508      1.333333    Female 13.333334      2     12
## 609      1.333333      Male 16.000000      2     13
## 710      1.333333      Male 24.000000      2     14
## 811      1.333333      Male 11.666667      2     15

Now, we’ll create our wanted plot using ggplot.

plot<- ggplot(data=data2, aes(x = Trial.Number, y = Accuracy, group=.group, color=Condition, frame=.frame, cumulative=TRUE))+
  geom_line(lwd=1, alpha=0.8)+  #change line trasparency with alpha = x
  scale_x_continuous("Trial Number", breaks=c(1:17))+
  scale_y_continuous("Accuracy")+
  theme_classic()+
  theme(text = element_text(size = 25), axis.text.x = element_text(size=20), legend.position=c(.8,.2))

And finally, let’s animate the plot and save it!

# change the speed of the animation by chanding interval = x

gganimate(plot, interval = 0.05)
## geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?

# remove the title by frame by adding title_frame = FALSE
gganimate(plot, title_frame = FALSE,filename="animated_plot.gif",ani.height=500, ani.width=700, interval = 0.05) # save as gif
## geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?
## Output at: animated_plot.gif